package com.sendpo.zjx.sendpo.activity;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.services.AmapLocationService;
import com.sendpo.zjx.sendpo.utils.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class LocationMapActivity extends AppCompatActivity implements AMap.OnMyLocationChangeListener, AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener {

    private AMap aMap = null;
    private String address = "";
    public AmapLocationService amapLocationService;
    private String area = "";
    private String city = "";
    GeocodeSearch geocoderSearch = null;
    public boolean isFirst = true;
    private String mCurrentLat = "";
    private String mCurrentLon = "";
    @BindView(R.id.mapview)
    MapView mapview;
    private String province = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_locationopen);
        Tools.initStatus(this, R.color.black);
        ButterKnife.bind(this);
        this.mapview.onCreate(savedInstanceState);
        if (this.aMap == null) {
            this.aMap = this.mapview.getMap();
        }
        initComponent();
        this.amapLocationService = new AmapLocationService(this);
        this.amapLocationService.registerListener(this);
        this.amapLocationService.setmLocationOption(getDefaultAmapLocationOptions());
        this.amapLocationService.start();
    }
    @OnClick(value = {R.id.backbt,R.id.bottom})
    public void  onMyClick(View view){
        switch (view.getId()){
            case R.id.backbt:
                finish();
                break;
            case R.id.bottom:
                if ((this.mCurrentLat.equals("")) || (this.mCurrentLon.equals("")))
                {
                    Toast.makeText(getApplicationContext(), "地图正在加载中", Toast.LENGTH_SHORT).show();
                    return;
                }
              Intent  paramView = new Intent();
                paramView.putExtra("province", "" + this.province);
                paramView.putExtra("city", "" + this.city);
                paramView.putExtra("area", "" + this.area);
                paramView.putExtra("address", "" + this.address);
                paramView.putExtra("mCurrentLat", "" + this.mCurrentLat);
                paramView.putExtra("mCurrentLon", "" + this.mCurrentLon);
                setResult(RESULT_OK, paramView);
                finish();
                break;
        }
    }
    protected void onDestroy()
    {
        super.onDestroy();
        this.mapview.onDestroy();
    }
    protected void onResume()
    {
        super.onResume();
        this.mapview.onResume();
    }

    protected void onSaveInstanceState(Bundle paramBundle)
    {
        super.onSaveInstanceState(paramBundle);
        this.mapview.onSaveInstanceState(paramBundle);
    }

    public AMapLocationClientOption getDefaultAmapLocationOptions() {
        AMapLocationClientOption localAMapLocationClientOption = null;
        if (0 == 0)
        {
            localAMapLocationClientOption = new AMapLocationClientOption();
            localAMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            localAMapLocationClientOption.setLocationCacheEnable(true);
            localAMapLocationClientOption.setOnceLocationLatest(false);
            localAMapLocationClientOption.setNeedAddress(true);
            localAMapLocationClientOption.setWifiScan(true);
            localAMapLocationClientOption.setInterval(2000L);
            localAMapLocationClientOption.setHttpTimeOut(20000L);
        }
        return localAMapLocationClientOption;
    }

    private void initComponent() {
        this.aMap.getUiSettings().setLogoBottomMargin(-50);
        this.geocoderSearch = new GeocodeSearch(this);
        this.geocoderSearch.setOnGeocodeSearchListener(this);
        this.aMap.showIndoorMap(true);
        this.aMap.getUiSettings().setCompassEnabled(false);
        this.aMap.setMyLocationEnabled(true);
        this.aMap.getUiSettings().setMyLocationButtonEnabled(false);
        this.aMap.getUiSettings().setScaleControlsEnabled(true);
        Object localObject = new MyLocationStyle();
        ((MyLocationStyle)localObject).radiusFillColor(getResources().getColor(R.color.translucent));
        ((MyLocationStyle)localObject).strokeWidth(0.0F);
        ((MyLocationStyle)localObject).strokeColor(getResources().getColor(R.color.translucent));
        ((MyLocationStyle)localObject).myLocationType(5);
        ((MyLocationStyle)localObject).interval(1000L);
        this.aMap.setMyLocationStyle((MyLocationStyle)localObject);
        this.aMap.setMyLocationEnabled(true);
        this.aMap.setOnMyLocationChangeListener(this);
        this.aMap.setTrafficEnabled(true);
        this.aMap.setMapType(1);
       this.aMap.setOnMapClickListener(new AMap.OnMapClickListener() {
        public void onMapClick(LatLng lng) {
            RegeocodeQuery  query = new RegeocodeQuery(new LatLonPoint(lng.latitude, lng.longitude), 200.0F, "autonavi");
           geocoderSearch.getFromLocationAsyn(query);
        }
    });
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation == null) {
            return;
        }
        this.province = aMapLocation.getProvince();
        this.city = aMapLocation.getCity();
        this.area = aMapLocation.getDistrict();
        this.mCurrentLat = (aMapLocation.getLatitude() + "");
        this.mCurrentLon = (aMapLocation.getLongitude() + "");
        int i = aMapLocation.getAddress().lastIndexOf(this.province + this.city + this.area);
        this.address = aMapLocation.getAddress().substring((this.province + this.city + this.area).length() + i, aMapLocation.getAddress().length());

    }

    @Override
    public void onMyLocationChange(Location location) {
             if(location==null){
             return;
             }
             if(isFirst){
                 this.aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 21.0F));
                 isFirst=false;
             }
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (i != 1000) {
            Toast.makeText(getApplicationContext(), "无法获取当前点击坐标", Toast.LENGTH_SHORT).show();
            return;
        }
        RegeocodeAddress paramRegeocodeResult = regeocodeResult.getRegeocodeAddress();
       int paramInt = paramRegeocodeResult.getFormatAddress().lastIndexOf(paramRegeocodeResult.getProvince() + paramRegeocodeResult.getCity() + paramRegeocodeResult.getDistrict());
        String str = paramRegeocodeResult.getFormatAddress().substring((paramRegeocodeResult.getProvince() + paramRegeocodeResult.getCity() + paramRegeocodeResult.getDistrict()).length() + paramInt, paramRegeocodeResult.getFormatAddress().length());
        Intent localIntent = new Intent();
        localIntent.putExtra("province", "" + paramRegeocodeResult.getProvince());
        localIntent.putExtra("city", "" + paramRegeocodeResult.getCity());
        localIntent.putExtra("area", "" + paramRegeocodeResult.getDistrict());
        localIntent.putExtra("address", "" + str);
        localIntent.putExtra("mCurrentLat", "" + this.mCurrentLat);
        localIntent.putExtra("mCurrentLon", "" + this.mCurrentLon);
        setResult(RESULT_OK, localIntent);
        Toast.makeText(getApplicationContext(), "" + paramRegeocodeResult.getProvince() + paramRegeocodeResult.getCity() + paramRegeocodeResult.getDistrict() + str, Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }
    protected void onPause()
    {
        super.onPause();
        this.mapview.onPause();
    }

}
